package com.cloud.logger.zchain.zchainclient.listener;

import com.alibaba.fastjson.JSONObject;
import com.cloud.logger.zchain.core.InetAddressUtilities;
import com.cloud.logger.zchain.core.common.entity.AngelaLoggerProjectNode;
import com.cloud.logger.zchain.core.common.properties.ServerInfo;
import com.cloud.logger.zchain.core.common.threadlocal.LoggerThreadLocal;
import com.cloud.logger.zchain.core.constants.Constants;
import com.cloud.logger.zchain.core.zookeeper.LeaderElection;
import com.cloud.logger.zchain.core.zookeeper.ZchainConnectionStateListener;
import com.cloud.logger.zchain.core.zookeeper.ZookeeperFactory;
import com.cloud.logger.zchain.zchainclient.util.AngelaAopLogger;
import com.cloud.logger.zchain.zchainclient.util.KafkaUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;
import org.springframework.util.StringUtils;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Timer;
import java.util.TimerTask;

/**
 * User: wcy
 * Date: 2017/11/8
 * Time: 16:03
 */
public class AngelaInit implements ServletContextListener {

    private int isStart = 0;

    static PropertiesConfiguration config;
    static {
        config = null;
        try {
            config = new PropertiesConfiguration("/application.properties");
        } catch (ConfigurationException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void contextInitialized(ServletContextEvent event) {
        if (isStart == 0) {
            initServer();
            ZookeeperFactory zookeeperFactory = ZookeeperFactory.getInstance();
            try {
                zookeeperFactory.init("115.159.160.97:2181","zchain-client",5000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            KafkaUtil.startProject();

            AngelaLoggerProjectNode project = LoggerThreadLocal.getCurrentProject();
            project.setIp(ServerInfo.ip);
            project.setPort(ServerInfo.port+"");

            LoggerThreadLocal.setZchainProject(project);

            //已启动状态
            isStart = 1;
            try {
                zookeeperFactory.createProjectNode(project);
                System.out.println("zchain zookeeper connect sucess");
            } catch (Exception e) {
                System.out.println("zchain zookeeper connect error");
            }
            //当spring容器初始化完成后就会执行该方法。
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                public void run() {
                    KafkaUtil.checkKafka();
                }
            }, 60 * 1000, 20*60 * 1000);

        }
    }

    private void initServer() {
        try {
            ServerInfo.setScenic(config.getString("zchain.project.scenic"));
            ServerInfo.setProject(config.getString("zchain.project.name"));
            ServerInfo.setIp(InetAddressUtilities.getLocalHostLANAddress().getHostAddress());
            ServerInfo.setPort(InetAddressUtilities.getTomcatPort());


            String success = config.getString("zchain.code.success");
            if(!StringUtils.isEmpty(success)){
                String[] codeArr = success.split(",");
                AngelaAopLogger.RESULT_SUCCESS = new HashSet<String>(Arrays.asList(codeArr));
            }
            String code = config.getString("zchain.code.name");
            AngelaAopLogger.RESULT_CODE = StringUtils.isEmpty(code)?AngelaAopLogger.RESULT_CODE:code;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void contextDestroyed(ServletContextEvent event) {
    }
}
